#ifndef BOARD_H__
#define BOARD_H__


/* ------------ Global Config --------- */
#define USE_STM32_F103

#ifdef USE_STM32_F103
    #include "stm32f10x.h"
#endif  // USE_STM32_F103


#include "bsp_types.h"
#include "SoftTimer.h"


/* ------------ Interrupt defines --------- */
#ifndef interrupt
    #define interrupt 
#endif  // interrupt


/* ------------ USART and modbus config --------- */
#define USART_MB        USART3

#define PORT_MB_TX      GPIOB
#define PIN_MB_TX       GPIO_Pin_10
#define PORT_MB_RX      GPIOB
#define PIN_MB_RX       GPIO_Pin_11

//#define MB_BAUDRATE     9600

#define USART_IRQN_MB   USART3_IRQn
#define USART_MB_ISR    USART3_IRQHandler
//#define TIM_MB_ISR      SysTick_Handler


/* ----------- Advanced Timer for H-Bridge PWM ------------------*/
#define TIM_HB_A      TIM1
#define TIM_HB_B      TIM8

#define PORT_HB_PWM1A   GPIOA
#define PIN_HB_PWM1A    GPIO_Pin_8

#define PORT_HB_PWM2A   GPIOA
#define PIN_HB_PWM2A    GPIO_Pin_9

#define PORT_HB_PWM3A   GPIOA
#define PIN_HB_PWM3A    GPIO_Pin_10

#define PORT_HB_PWM4A   GPIOA
#define PIN_HB_PWM4A    GPIO_Pin_11

#define PORT_HB_PWM1B   GPIOC
#define PIN_HB_PWM1B    GPIO_Pin_6

#define PORT_HB_PWM2B   GPIOC
#define PIN_HB_PWM2B    GPIO_Pin_7

#define PORT_HB_PWM3B   GPIOC
#define PIN_HB_PWM3B    GPIO_Pin_8

#define PORT_HB_PWM4B   GPIOC
#define PIN_HB_PWM4B    GPIO_Pin_9


/* ----------- General Timer for Encoder ------------------*/
#define TIM_ENC1                 TIM2
#define TIM_ENC1_IRQN            TIM2_IRQn
#define TIM_ENC1_IRQ_ISR         TIM2_IRQHandler

#define PORT_ENC1A     GPIOA
#define PIN_ENC1A      GPIO_Pin_15
#define PORT_ENC1B     GPIOB
#define PIN_ENC1B      GPIO_Pin_3


#define TIM_ENC2                 TIM3
#define TIM_ENC2_IRQN            TIM3_IRQn
#define TIM_ENC2_IRQ_ISR         TIM3_IRQHandler

#define PORT_ENC2A     GPIOA
#define PIN_ENC2A      GPIO_Pin_6
#define PORT_ENC2B     GPIOA
#define PIN_ENC2B      GPIO_Pin_7


#define TIM_ENC3                 TIM4
#define TIM_ENC3_IRQN            TIM4_IRQn
#define TIM_ENC3_IRQ_ISR         TIM4_IRQHandler

#define PORT_ENC3A     GPIOB
#define PIN_ENC3A      GPIO_Pin_6
#define PORT_ENC3B     GPIOB
#define PIN_ENC3B      GPIO_Pin_7


#define TIM_ENC4                 TIM5
#define TIM_ENC4_IRQN            TIM5_IRQn
#define TIM_ENC4_IRQ_ISR         TIM5_IRQHandler

#define PORT_ENC4A     GPIOA
#define PIN_ENC4A      GPIO_Pin_0
#define PORT_ENC4B     GPIOA
#define PIN_ENC4B      GPIO_Pin_1

/* ----------- GPIO for motor control ------------------*/


/* ----------- GPIO for Tets Point ------------------*/
#define PORT_TP1 GPIOB
#define PIN_TP1  GPIO_Pin_13
#define PORT_TP2 GPIOB
#define PIN_TP2  GPIO_Pin_14


/* ----------- GPIO for LEDs ------------------*/
#define PORT_LED_0    GPIOB
#define PIN_LED_0     GPIO_Pin_12


/* ----------- Exported Function Marcos ------------------*/
#define TP1_HIGH    GPIO_SetBits(PORT_TP1, PIN_TP1)
#define TP1_LOW     GPIO_ResetBits(PORT_TP1, PIN_TP1);
#define TP2_HIGH    GPIO_SetBits(PORT_TP2, PIN_TP2)
#define TP2_LOW     GPIO_ResetBits(PORT_TP2, PIN_TP2);

#define LED_0_ON    GPIO_ResetBits(PORT_LED_0, PIN_LED_0);
#define LED_0_OFF   GPIO_SetBits(PORT_LED_0, PIN_LED_0);


/* ------------ Exported Functions --------- */
// ===== hardware =====
void initUsartMB(uint32_t baud);
void initTimMB_us(uint32_t period_us);
void disableSysTick(void);
void enableSysTick(void);

void initBoard(void);
void board_EnableAllClocks(void);
void board_ConfigAllClocks(void);

// ===== utilities =====
int32_t s32abs(int32_t x);

#endif  // BOARD_H__
